package com.etheart.usersphere.action.user;

import java.util.Map;

import org.apache.struts2.interceptor.ApplicationAware;
import org.apache.struts2.interceptor.RequestAware;
import org.apache.struts2.interceptor.SessionAware;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.etheart.model.entity.User;
import com.etheart.usersphere.service.UserService;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.inject.Inject;

@Controller("passwordAction")
@Scope("prototype")
public class PasswordAction extends ActionSupport implements SessionAware,RequestAware ,ApplicationAware{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private User user;
	private String oldPass;
	private String newPass;
	@Inject("usersphere_UserService")
	private UserService userService;
	
	public String loginConfirm() {
		User u = userService.getLogin(user.getUsername(),user.getPassword());
		if (u != null) {
			session.put("userid", u.getId());
			request.put("message", "登录成功");
			return "success";
		}else{
			request.put("message", "登录失败，密码不正确");
			return "login";
		}
	}
	
	public String modify() {
		return "success";
	}
	
	public String modifyConfirm(){
		Long userid = (Long) session.get("userid");
		if (userid == null || userid == 0) {
			request.put("message", "您还未登录,请登录");
			return "login";
		}
		User u = userService.getUserById(userid);
		if (userService.matchingPassword(u,oldPass)) {
			userService.modifyPassword(u,newPass);
			session.put("userid", u.getId());
			request.put("message", "密码修改成功");
		}else{
			request.put("message", "旧密码不正确");
		}
		return "message";
	}
	
	
	Map<String, Object> request;
	Map<String, Object> session;
	Map<String, Object> application;
	public void setRequest(Map<String, Object> arg0) {
		this.request = arg0;
	}
	public void setSession(Map<String, Object> arg0) {
		this.session = arg0;
	}
	public void setApplication(Map<String, Object> arg0) {
		this.application=arg0;
	}
}
